package com.gongpingjialoan.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.gongpingjialoan.R;
import com.gongpingjialoan.bean.LoabHistoryBean;
import com.gongpingjialoan.utility.ArithUtil;
import com.gongpingjialoan.utility.TimeUtils;

import java.util.List;

/**
 * Created by Administrator on 2017/10/19.
 */

public class LoanHistoryAdapter extends BaseAdapter {

    private List<LoabHistoryBean.DataBean> mRecordsBeans;

    private Context mContext;

    private LayoutInflater mLayoutInflater;

    public LoanHistoryAdapter(List<LoabHistoryBean.DataBean> recordsBeans, Context context) {
        mRecordsBeans = recordsBeans;
        mContext = context;
        mLayoutInflater = LayoutInflater.from(context);
    }

    public void setData(List<LoabHistoryBean.DataBean> recordsBeans) {
        this.mRecordsBeans = recordsBeans;
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return mRecordsBeans.size();
    }

    @Override
    public Object getItem(int position) {
        return mRecordsBeans.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.item_loan_history, null);
            holder = new ViewHolder();
            holder.model_zh = (TextView) convertView.findViewById(R.id.model_zh);
            holder.credit_limit = (TextView) convertView.findViewById(R.id.credit_limit);
            holder.kind = (TextView) convertView.findViewById(R.id.kind);
            holder.created_at = (TextView) convertView.findViewById(R.id.created_at);
            holder.status = (TextView) convertView.findViewById(R.id.status);
            convertView.setTag(R.id.tag_first, holder);
        } else {
            holder = (ViewHolder) convertView.getTag(R.id.tag_first);
        }
        LoabHistoryBean.DataBean source = mRecordsBeans.get(position);
        convertView.setTag(R.id.tag_second, source);
        if (source != null) {
            holder.model_zh.setText(source.getModel_zh());
            holder.kind.setText(source.getKind());
            holder.status.setText(source.getStatus());
            Double credit_limit = source.getCredit_limit();
            java.text.DecimalFormat df = new java.text.DecimalFormat("##,###,###");
            if (credit_limit != null){
                String str = df.format(credit_limit);
                if ("车抵贷".equals(source.getKind())){
                    if (str.length()>3){
//                        str = str.substring(0,str.length()-3) + "000";
                        str = df.format(ArithUtil.mul(ArithUtil.setScale(ArithUtil.mul(credit_limit,0.0001),1),10000d));
                    }
                }
                holder.credit_limit.setText(str+"元");
            }else {
                holder.credit_limit.setText("");
            }
            String created_at = source.getCreated_at();
            if (!TextUtils.isEmpty(created_at)){
                Double data = Double.parseDouble(created_at);
                holder.created_at.setText(TimeUtils.timeYearMonthDayFormat(data.longValue()*1000));
            }else {
                holder.created_at.setText("");
            }
        }
        return convertView;
    }


    public static class ViewHolder {

        public TextView model_zh;

        public TextView credit_limit;

        public TextView kind;

        public TextView created_at;

        public TextView status;

    }
}